<?php

if(!defined('IN_JANSEN')) {
    exit('Access Denied');
}

class cart {
    /*
  
    * $myCart[5][$name]:��Ʒ���Ϊ5������
    * $myCart[5][$price]:��Ʒ���Ϊ5�ĵ���
    * $myCart[5][$count]:��Ʒ���Ϊ5������
    * $myCart[5][$cost]:��Ʒ���Ϊ5�ĺϼƽ��
    */

    var $sortCount;    //��Ʒ������
    var $totalCost;    //��Ʒ�ܽ��

    var $myCart;
    var $Id;           //ÿ����Ʒ��ID
    var $Code;         //ÿ����Ʒ������
    var $Name;         //ÿ����Ʒ������
    var $Price;        //ÿ����Ʒ�ļ۸�
    var $Cbm;          
    var $Cuft;        
    var $Count;        //ÿ����Ʒ�ļ���
    var $Cost;         //ÿ����Ʒ�ļ�ֵ
    var $inType;       //from product or inventory
	var $fitemid; //Finishѡ��ID
	var $fabid; //fabricѡ��ID
	var $leaid; //leatherѡ��ID


   
    function cart() {
        $this->sortCount = 0;
        $this->totalCost = 0;
        $this->myCart = array();
        $this->Id = 0;
        $this->Code = '';
        $this->Name = '';
        $this->Price = 0;
        $this->Cbm = 0;
        $this->Cuft = 0;
        $this->Count = 0;
		$this->Cost = 0;
		$this->inType = 0;
		$this->fitemid = 0;
		$this->fabid = 0;
		$this->leaid = 0;

        if(!session_is_registered("myCart")) {
            session_register('myCart');
        }
        $this->update();
    }

    // private������session��ֵ����������Ӧ����
    function update() {
        $myCart = $_SESSION["myCart"];
        if(false==$myCart ) {
            $this->sortCount = 0;
            $this->totalCost = 0;
            $this->myCart = array();
            $this->Id = 0;
			$this->Code = '';
            $this->Name = '';
            $this->Price = 0;
            $this->Cbm = 0;
            $this->Cuft = 0;
            $this->Count = 0;
            $this->Cost = 0;
			$this->inType = 0;
			$this->fitemid = 0;
			$this->fabid = 0;
		    $this->leaid = 0;
            
            return false;
        }
        
        //�õ���Ʒ��������
        $this->sortCount=count($myCart);
        
        if($this->sortCount>0) {
          
            $totalCost = 0    ;
			$num_ary = array('cbm','cuft','price','totalcost');
            foreach($myCart as $key=>$val) {
              
                foreach($val as $proName=>$proVal) {
                    if(in_array($proName, $num_ary)) {
                        $val[$proName] = round(eregi_replace(",", "",$proVal),2);
                        $myCart[$key][$proName] = $val[$proName];
                    }
                }

                
                $myCart[$key]["cost"] = round($val["count"]*$val["price"], 2);
                
             
                $totalCost += $myCart[$key]["cost"];
            }
            
            $this->totalCost = $totalCost;
            $this->myCart = $myCart;
            $_SESSION["myCart"] = $myCart;			
        }
    }

    // ��ʽ������Ϊ��������
    function formatNum($data) {
        foreach($data as $key=>$val) {
            foreach($val as $sName=>$sValue) {
                if($sName !="name") {
                    $data[$key][$sName] = number_format($sValue, 2);
                }
            }
        }
        return $data;
    }

  
    function addOne($id,$code,$name,$price,$cbm,$cuft,$fitemid,$intype=0,$fabid=0,$leaid=0)    {
        //$id = $tp == 0 ? 'product_' . $id : 'inventory_' . $id;
		$key = $intype . '_' . $id . '_'.$fitemid.'_'.$fabid.'_'.$leaid;
        $myCart = $_SESSION["myCart"];
        if (!isset($myCart[$key]['count'])) {
            $myCart[$key]['count'] = 0;
        }
        
        //���ù��ﳵ�е�����
		$myCart[$key]["id"]  = $id;
		$myCart[$key]["code"]  = $code;
		$myCart[$key]["name"]  = $name;
		$myCart[$key]["price"] = $price;
		$myCart[$key]["cbm"] = $cbm;
		$myCart[$key]["cuft"] = $cuft;
		$myCart[$key]["intype"] = $intype;
		$myCart[$key]["fitemid"] = $fitemid;
		$myCart[$key]["fabid"] = $fabid;
		$myCart[$key]["leaid"] = $leaid;

		
        ++$myCart[$key]["count"];
        $_SESSION["myCart"] = $myCart;
        
        //����һ����ĳ�Ա����
        $this->update();
    }
    
    /**
    * ���ﳵ�����һ����Ʒ,���û�У�������ӣ�����Ѿ����ڣ������Ϊdata
    * @param $data  - Ҫ��ӵ���Ʒ,��ʽΪ��
    *                 $data[0][id],   $data[0][name],
    *                 $data[0][price],$data[0][count],$date[0][intype]
    * @return boolean
    */
    function addData($data)
    {
        if(count($data > 0)) {
            
            $myCart = $_SESSION["myCart"];
            foreach($data as $val) {
                extract($val);
				$fitemid = (int)$fitemid;
				$intype = (int)$intype;
				$fitemid = (int)$fitemid;
				$fabid = (int)$fabid;
				$leaid = (int)$leaid;
				$key = $intype . '_' . $id . '_'.$fitemid.'_'.$fabid.'_'.$leaid;
                //���ù��ﳵ�е�����
                $myCart[$key]["id"]  = $id;
                $myCart[$key]["code"]  = $code;
                $myCart[$key]["name"]  = $name;
                $myCart[$key]["price"] = $price;
                $myCart[$key]["cbm"] = $cbm;
                $myCart[$key]["cuft"] = $cuft;
				$myCart[$key]["count"] = $count;
				$myCart[$key]["intype"] = $intype;
				$myCart[$key]["fitemid"] = $fitemid;
				$myCart[$key]["fabid"] = $fabid;
		        $myCart[$key]["leaid"] = $leaid;
            }
            
            $_SESSION["myCart"] = $myCart;
            
            //����һ����ĳ�Ա����
            $this->update();
        }
    }
    
    // ����һ����Ʒ�ĵ���
    function updatePrice($id, $price) {
        if($price <0)  return false;
        
        $myCart = $_SESSION["myCart"];
        if($myCart[$id]==true) {
            $myCart[$id]["price"]=$price;
            $_SESSION["myCart"] = $myCart;
            $this->update();
        }
    }
    
    // ��һ����Ʒ��������1
    function removeOne($id) {
        $count = $this->myCart[$id]["count"];
        if($count>0) {
            $this->modifyCount($id, --$count);
        }
    }

    //�ı���Ʒ�ĸ���,������뵥�ۣ���һ����ĵ���
    function modifyCount($id, $ncount, $price=0) {
        if($ncount <= 0) return false;
        $myCart = $_SESSION["myCart"];
        
        //$id = $tp . '_' . $id;
        
        if($myCart[$id]==true) {
            $myCart[$id]["count"]=$ncount;
            
            //����д��뵥�ۣ���һ����ĵ���
            if($price >0 ) $myCart[$id]["price"]=$price;

            $_SESSION["myCart"] = $myCart;
            
            $this->update();
        }

    }

    //���һ����Ʒ
    function emptyOne($i) {
        $myCart = $_SESSION["myCart"];
        unset($myCart[$i]);
        if(count($myCart)==0) {
            $this->emptyAll();
        } else {
            $_SESSION["myCart"] = $myCart;
            $this->update();
        }
    }

    // ������е���Ʒ
    function emptyAll() {
		$_SESSION["myCart"] = "";
        $this->update();
		//echo $this->getSortCount();exit;
		
        session_unset($_SESSION["myCart"]);
        session_destroy();
        cookie(session_name(), '', 0);
    }

    // �������й��ﳵ�е�����
    function getData() {
        if($this->sortCount > 0) {
            return $this->myCart;
        }else{
            return array();
        }
    }
    
    //ȡһ����Ʒ����Ϣ������array('id','name','price','count','cost');
    function getOne($k) {
        $data = $this->myCart[$k];
        if(false==$data) return array();

		$ary = explode('_',$k);
		if(count($ary) > 2)
		{
			$data["id"] = $ary[1];
		}
        return $data;
    }

    //ȡ�ܵ���Ʒ������
    function getSortCount() {
        return $this->sortCount;
    }

    //ȡ�ܵ���Ʒ��ֵ
    function getTotalCost() {
        return $this->totalCost;
    }

	//��֤id�Ƿ��Ѿ�����
	function isKeyExists($id)
	{
		return array_key_exists($id,$this->myCart);	
	}
	//����Finish item
	function modifyKey($oldid,$new_fitemid,$num)
	{
		if(!is_numeric($num)) return;
		
		$aryTmp = explode('_',$oldid);
		array_pop($aryTmp);
		$aryTmp[]=$new_fitemid;
		$newid = implode('_',$aryTmp);
	
		if($this->isKeyExists($newid))
		{
			$this->emptyOne($oldid);
			$data = $this->getOne($newid);			
			$this->modifyCount($newid,$data["count"]+$num,$data["price"]);
		}
		else
		{
			$myCart = $_SESSION["myCart"];			
			$data = $myCart[$oldid];
			unset($myCart[$oldid]);
			if(!$data) return;
			
			$data["count"] = $num;
			$data["fitemid"] = $new_fitemid;
			$myCart[$newid] = $data;			
			
			$_SESSION["myCart"] = $myCart;
						
			$this->update();
		}
	}
	function modifyAllKey($oldid,$new_fitemid,$new_fabid,$new_leaid,$num)
	{
		if(!is_numeric($num)) return;

		$myCart = $_SESSION["myCart"];	
		
		$aryTmp = explode('_',$oldid);

		$aryTmp[2]=$new_fitemid;
		$aryTmp[3]=$new_fabid?$new_fabid:0;
		$aryTmp[4]=$new_leaid?$new_leaid:0;		
		$newid = implode('_',$aryTmp);
		$data = $myCart[$oldid];	
		unset($myCart[$oldid]);
		if($this->isKeyExists($newid) AND $newid != $oldid)
		{
			$num = $data['count'] + $num;
			$this->modifyCount($newid,$num,$data["price"]);
		}
		else
		{
			if(!$data) return;
			
			$data["count"] = $num;
			$data["fitemid"] = $new_fitemid;
			$data["leaid"] = $new_leaid;
			$data["fabid"] = $new_fabid;
			$myCart[$newid] = $data;			
		
			$_SESSION["myCart"] = $myCart;						
			$this->update();
		}
	}
	public function get_key($data) {

		if(empty($data)) return '';

		return (int)$data['intype'].'_'.(int)$data['id'].'_'.(int)$data['fitemid'].'_'.(int)$data['fabid'].'_'.(int)$data['leaid'];
	}
}
?>
